<?php

namespace wxutils;

class Index
{
  public static function getToken($url)
  {
      $ch = curl_init();
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HEADER,0);
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22");
      curl_setopt($ch, CURLOPT_ENCODING,'gzip');
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
      $output = curl_exec($ch);
      curl_close($ch);
      return $output;
  }

  public static function getWxUserInfo($appid,$secret,$code)
  {
      $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
      $user_data = json_decode(self::getToken($url),true);
      if(isset($user_data['access_token'],$user_data['openid']))
      {
          $token    = $user_data['access_token'];
          $openid   = $user_data['openid'];
          $userinfo = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid.'&lang=zh_CN';
          return json_decode(self::getToken($userinfo),true);
      }
      return false;
  }
}